<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use think\Controller;
use think\Config;
use think\Loader;
use think\Db;

class AdsController extends BaseController
{
    //主页
    public function index(){
        if (request()->isAjax()){
            $page=input("param.page",1);
            $limit=\input("param.limit",100);
            $sum=($page-1)*$limit;
            $keyword=\input("param.keyword");
            $where=[];
            if ($keyword){
                $where["a.title|ap.name"]=["like","%{$keyword}%"];
            }
            $f[]="ap.name ap_name";
            $f[]="a.*";
            $data=Db::name("ads_position")
                ->alias("ap")
                ->join("ads a","a.ads_position_id=ap.id","left")
                ->where($where)
                ->field($f)
                ->limit("$sum,$limit")
                ->order("a.id ASC")
                ->select();
            $num=Db::name("ads_position")
                ->alias("ap")
                ->join("ads a","a.ads_position_id=ap.id","left")
                ->where($where)
                ->field($f)
                ->order("a.id ASC")
                ->select();
            $result['code'] = 0;
            $result['msg'] = "";
            $result['count'] = count($num);
            $result['data'] = $data;
            return json($result);
        }
        return $this->fetch("index");
    }

    /*
     * 添加
     */
    public function add(){
        if (request()->isAjax()){
            $images=input("arr/a");  //接受数组格式的
            $data["title"]=input("param.title");
            $data["ads_position_id"]=input("param.ads_position_id");
            $data["link_url"]=input("param.link_url");
            $data["start_date"]=input("param.start_date");
            $data["end_date"]=input("param.end_date");
            $data["status"]=input("param.status");
            $data["sort"]=input("param.sort");
            $data["create_time"]=date("Y-m-d H:i:s");
            $data["update_time"]=date("Y-m-d H:i:s");
            foreach ($images as $v){
                $data["images"]=$v;
                $result=Db::name("ads")->insert($data);
            }
            if ($result){
                return json(["code"=>1,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>0,"msg"=>"添加失败"]);
            }
        }
        //读取广告位
        $adsp=Db::name("ads_position")->select();
        $this->assign("adsp",$adsp);
        return $this->fetch("add");
    }
    /*
     * 编辑
     */
    public function edit(){
        $id=input("param.id");
        if (request()->isAjax()){
            $data["id"]=input("param.id");
            $data["images"]=input("param.arr");  //接受数组格式的
            $data["title"]=input("param.title");
            $data["link_url"]=input("param.link_url");
            $data["start_date"]=input("param.start_date");
            $data["end_date"]=input("param.end_date");
            $data["status"]=input("param.status");
            $data["sort"]=input("param.sort");
            $data["update_time"]=date("Y-m-d H:i:s");
            $result=Db::name("ads")->where(["id"=>$data["id"]])->update($data);
            if ($result){
                return json(["code"=>1,"msg"=>"编辑成功"]);
            }else{
                return json(["code"=>0,"msg"=>"编辑失败"]);
            }

        }
        $find=Db::name("ads")->where(["id"=>$id])->find();
        $this->assign("find",$find);
        return $this->fetch("edit");
    }

    /*
     * 删除
     */
    public function del(){
        $id=input("param.id");
        $result=Db::name("ads")->where(["id"=>$id])->delete();
        if ($result){
            writelog("删除广告成功");
            return json(["code"=>1,"msg"=>"删除成功"]);
        }else{
            return json(["code"=>0,"msg"=>"删除失败"]);
        }
    }
}